import clr, sys

clr.AddReferenceByPartialName("Microsoft.Office.Interop.Outlook")
clr.AddReferenceByPartialName("office")

from Microsoft.Office.Interop import Outlook
from Microsoft import Office

from System.Reflection import *
from System.IO import *
from System import *

from ActiveXObject import *
from GtdProperties import *
from Globals import *
import Settings

def ArchiveMailItem(mailItem):
    folder = FindFolderByFullPath(mailItem.getSession().Folders, \
                                  Settings.archiveFolder)
    if folder != None:
        mailItem.Move(folder)

def Defer(mailItem):
    folder = FindFolderByFullPath(mailItem.getSession().Folders, \
                                  Path.Combine(Settings.gtdFolder, Settings.deferFolder))
    if folder != None:
        id = NewId()
        item = mailItem.Move(folder)
        mailItem = ActiveXObject(item)
        gtdProperties = GtdProperties(ActiveXObject(mailItem.getUserProperties()))
        gtdProperties.setGtdId(id)
        gtdProperties.setCompleted(False)
        gtdProperties.setGtd(True)
        gtdProperties.setGtdLinkFolder(mailItem.getSession().GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).FullFolderPath)
        gtdProperties.setGtdLinkType("AppointmentItem")
        mailItem.Save()

        appointment = ActiveXObject(mailItem.getSession().Application.CreateItem(Outlook.OlItemType.olAppointmentItem))
        appointment.setSubject(mailItem.getSubject())
        appointment.setBody("From: %s\nSent: %s\nTo: %s\nCc: %s\nSubject: %s\n\n%s" % \
                            (mailItem.getSenderName(), \
                             mailItem.getReceivedTime().ToString(), \
                             mailItem.getTo(), \
                             mailItem.getCC(), \
                             mailItem.getSubject(), \
                             mailItem.getBody()))
        gtdProperties = GtdProperties(ActiveXObject(appointment.getUserProperties()))
        gtdProperties.setGtdId(id)
        gtdProperties.setCompleted(False)
        gtdProperties.setGtd(True)
        gtdProperties.setGtdLinkFolder(folder.FullFolderPath)
        gtdProperties.setGtdLinkType("MailItem")
        appointment.Display(False)

def NextAction(mailItem):
    folder = FindFolderByFullPath(mailItem.getSession().Folders, \
                                  Path.Combine(Settings.gtdFolder, Settings.actionFolder))
    if folder != None:
        item = mailItem.Move(folder)
        mailItem = ActiveXObject(item)

        id = NewId()
        gtdProperties = GtdProperties(ActiveXObject(mailItem.getUserProperties()))
        gtdProperties.setGtdId(id)
        gtdProperties.setCompleted(False)
        gtdProperties.setGtd(True)
        gtdProperties.setGtdLinkFolder(mailItem.getSession().GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks).FullFolderPath)
        gtdProperties.setGtdLinkType("TaskItem")

        mailItem.Save()

        task = ActiveXObject(mailItem.getSession().Application.CreateItem(Outlook.OlItemType.olTaskItem))
        task.setSubject(mailItem.getSubject())
        task.setBody("From: %s\nSent: %s\nTo: %s\nCc: %s\nSubject: %s\n\n%s" % \
                     (mailItem.getSenderName(), \
                      mailItem.getReceivedTime().ToString(), \
                      mailItem.getTo(), \
                      mailItem.getCC(), \
                      mailItem.getSubject(), \
                      mailItem.getBody()))
        gtdProperties = GtdProperties(ActiveXObject(task.getUserProperties()))
        gtdProperties.setGtdId(id)
        gtdProperties.setCompleted(False)
        gtdProperties.setGtd(True)
        gtdProperties.setGtdLinkFolder(folder.FullFolderPath)
        gtdProperties.setGtdLinkType("MailItem")
        task.Display(False)
        
